﻿@page "/weather"
@using BlazorApp1.Data
@using Microsoft.EntityFrameworkCore
@attribute [StreamRendering]
@rendermode InteractiveServer

@inject ApplicationDbContext Context;
@inject LoanService LoanService;

<PageTitle>设备管理</PageTitle>

<h1>设备管理</h1>

@if (forecasts == null)
{
    <p>
        <em>Loading...</em>
    </p>
}
else
{
    <div class="row">
        <div class="col-md-3">
            <label>设备Id: </label>
            <input class="form-control" readonly="readonly" @bind="_newDevice.Id"/>
        </div>
        <div class="col-md-3">
            <label>设备名称: </label>
            <input class="form-control" @bind="_newDevice.Name"/>
        </div>
        <div class="col-md-3">
            <label>规格: </label>
            <input class="form-control" @bind="_newDevice.Specification"/>
        </div>
        <div class="col-md-3" >
            <label>信息: </label>
            <input class="form-control" @bind="_newDevice.Information"/>
        </div>
    </div>
    <div class="float-right">
        <button class="btn btn-primary" disabled="@(!AllFieldsFilled())" @onclick="AddDevice">添加设备</button>
    </div>
    <table class="table">
        <thead>
        <tr>
            <th>Id</th>
            <th>名称</th>
            <th>规格</th>
            <th>信息</th>
            <th>状态</th>
            <th>操作</th>
        </tr>
        </thead>
        <tbody>
        @foreach (var forecast in forecasts)
        {
            <tr>
                <td>@forecast.Id</td>
                <td>@forecast.Name</td>
                <td>@forecast.Specification</td>
                <td>@forecast.Information</td>
                <td>@(forecast.State ? "已租借" : "未租借")</td>
                <td><button class="btn btn-primary" @onclick="() => DeleteDevice(forecast)">删除</button></td>
            </tr>
        }
        </tbody>
    </table>
}

@code {
    private List<DeviceInfo> forecasts;

    protected override async Task OnInitializedAsync()
    {
        forecasts = await Context.DeviceInfo.ToListAsync();
        LoanService.Action = null;
        LoanService.Action += Action;
    }

    private void Action(string id)
    {
        if (id != _newDevice.Id)
        {
            InvokeAsync(() =>
            {
                _newDevice.Id = id;
                StateHasChanged();
            });
        }
    }

    DeviceInfo _newDevice = new();
    
    private async void AddDevice()
    {
        await Context.AddAsync(_newDevice);
        await Context.SaveChangesAsync();
        forecasts.Add(_newDevice);
        _newDevice = new DeviceInfo(); // Clear the input fields after adding
    }
    
    private async void DeleteDevice(DeviceInfo device)
    {
        Context.Remove(device);
        await Context.SaveChangesAsync();
        forecasts.Remove(device);
    }
    
    bool AllFieldsFilled()
    {
        return !string.IsNullOrWhiteSpace(_newDevice.Id) && 
               !string.IsNullOrWhiteSpace(_newDevice.Name) && 
               !string.IsNullOrWhiteSpace(_newDevice.Information) && 
               !string.IsNullOrWhiteSpace(_newDevice.Specification);
    }
}